package com.lishe.lishe.lishe.MyRecyclerView;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.annotation.ColorInt;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;

import com.lishe.lishe.lishe.utils.Utils;

/**
 * 针对RecyclerView 的gridItem宽度分配不均的问题处理
 * isAround：左右两边是否也需要等间隔
 * hasHeader: 是否有headview
 */
public class GridDividerItemDecoration extends RecyclerView.ItemDecoration {
    private Paint mPaint;
    private int mDividerWidth;
    private boolean isAround, hasHeader;
    public static int defaultColor = Color.parseColor("#00000000");

    public GridDividerItemDecoration(int space) {
        this(space, defaultColor, true, false);
    }

    public GridDividerItemDecoration(int space, boolean isHeader) {
        this(space, defaultColor, true, isHeader);
    }
    public GridDividerItemDecoration(int space, boolean isAround, boolean isHeader) {
        this(space, defaultColor, isAround, isHeader);
    }

    public GridDividerItemDecoration(int space, @ColorInt int color, boolean isArround, boolean hasHeader) {
        mDividerWidth = space;
        this.hasHeader = hasHeader;
        this.isAround = isArround;
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setColor(color);
        mPaint.setStyle(Paint.Style.FILL);
    }


    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        int itemPosition = ((RecyclerView.LayoutParams) view.getLayoutParams()).getViewLayoutPosition();
        parent.getAdapter();
        int spanCount = getSpanCount(parent);
        int childCount = parent.getAdapter().getItemCount();

        boolean isLastRow = isLastRow(parent, itemPosition, spanCount, childCount);
        int top = 0;
        int left = 0;
        int right = 0;
        int bottom = 0;
        if (hasHeader) {
            if (itemPosition == 0) {
                top = Utils.dp2px(5);
                bottom = Utils.dp2px(5);
            } else {//假如：divider = 15, count = 2
                int eachWidth = (spanCount - 1) * mDividerWidth / spanCount;// 7.5
                int dl = mDividerWidth - eachWidth;//7.5
                // 默认只有一个header
                if (itemPosition % spanCount == 1) {//最左边
                    left = isAround ? mDividerWidth : 0;
                    right = isAround ? itemPosition % spanCount * dl : eachWidth - left;
                } else if (itemPosition % spanCount == 0) {// 最右边
                    left = (itemPosition - 1) % spanCount * dl;
                    right = isAround ? mDividerWidth : 0;
                } else {
                    left = itemPosition % spanCount * dl;
                    right = mDividerWidth - left;
                }
                bottom = mDividerWidth;
            }
        } else {
            int eachWidth = (spanCount - 1) * mDividerWidth / spanCount;
            int dl = mDividerWidth - eachWidth;
            left = itemPosition % spanCount == 0 ? (isAround ? mDividerWidth : 0) : itemPosition % spanCount * dl;
            right = itemPosition % spanCount == 0 ? eachWidth : (isAround ? mDividerWidth : 0);
            bottom = mDividerWidth;
        }
        if (isLastRow) {
            bottom = 0;
        }
        outRect.set(left, top, right, bottom);

    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDraw(c, parent, state);
        draw(c, parent);
    }

    //绘制item分割线
    private void draw(Canvas canvas, RecyclerView parent) {
        int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            View child = parent.getChildAt(i);
            RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child.getLayoutParams();

            //画水平分隔线
            int left = child.getLeft();
            int right = child.getRight();
            int top = child.getBottom() + layoutParams.bottomMargin;
            int bottom = top + mDividerWidth;
            if (mPaint != null) {
                canvas.drawRect(left, top, right, bottom, mPaint);
            }
            //画垂直分割线
            top = child.getTop();
            bottom = child.getBottom() + mDividerWidth;
            left = child.getRight() + layoutParams.rightMargin;
            right = left + mDividerWidth;
            if (mPaint != null) {
                canvas.drawRect(left, top, right, bottom, mPaint);
            }
        }
    }

    private boolean isLastColumn(RecyclerView parent, int pos, int spanCount,
                                 int childCount) {
        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager) {
            if ((pos + 1) % spanCount == 0) {// 如果是最后一列，则不需要绘制右边
                return true;
            }
        } else if (layoutManager instanceof StaggeredGridLayoutManager) {
            int orientation = ((StaggeredGridLayoutManager) layoutManager)
                    .getOrientation();
            if (orientation == StaggeredGridLayoutManager.VERTICAL) {
                if ((pos + 1) % spanCount == 0)// 如果是最后一列，则不需要绘制右边
                {
                    return true;
                }
            } else {
                childCount = childCount - childCount % spanCount;
                if (pos >= childCount)// 如果是最后一列，则不需要绘制右边
                    return true;
            }
        }
        return false;
    }

    private boolean isLastRow(RecyclerView parent, int pos, int spanCount,
                              int childCount) {
        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager) {
            int lines = 0;
            if (hasHeader) {// c2 line =2,c3=l2
                lines = (childCount - 1) % spanCount == 0 ? ((childCount - 1) / spanCount) + 1 : (childCount - 1) / spanCount + 2;
            } else {
                lines = childCount % spanCount == 0 ? childCount / spanCount : childCount / spanCount + 1;
            }
            return lines == (!hasHeader ? pos / spanCount + 1 : (pos + 1) / spanCount + 1);
        } else if (layoutManager instanceof StaggeredGridLayoutManager) {
            int orientation = ((StaggeredGridLayoutManager) layoutManager)
                    .getOrientation();
            // StaggeredGridLayoutManager 且纵向滚动
            if (orientation == StaggeredGridLayoutManager.VERTICAL) {
                childCount = childCount - childCount % spanCount;
                // 如果是最后一行，则不需要绘制底部
                if (pos >= childCount)
                    return true;
            } else {
                // 如果是最后一行，则不需要绘制底部
                if ((pos + 1) % spanCount == 0) {
                    return true;
                }
            }
        }
        return false;
    }

    private boolean isFirstRow(RecyclerView parent, int pos, int spanCount,
                               int childCount) {
        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager) {
            if ((pos / spanCount + 1) == 1) {
                return true;
            } else {
                return false;
            }
        } else if (layoutManager instanceof StaggeredGridLayoutManager) {
            int orientation = ((StaggeredGridLayoutManager) layoutManager)
                    .getOrientation();
            // StaggeredGridLayoutManager 且纵向滚动
            if (orientation == StaggeredGridLayoutManager.VERTICAL) {
                childCount = childCount - childCount % spanCount;
                // 如果是最后一行，则不需要绘制底部
                if (pos >= childCount)
                    return true;
            } else {
                // 如果是最后一行，则不需要绘制底部
                if ((pos + 1) % spanCount == 0) {
                    return true;
                }
            }
        }
        return false;
    }

    //获取列数
    private int getSpanCount(RecyclerView parent) {
        int spanCount = -1;
        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager) {

            spanCount = ((GridLayoutManager) layoutManager).getSpanCount();
        } else if (layoutManager instanceof StaggeredGridLayoutManager) {
            spanCount = ((StaggeredGridLayoutManager) layoutManager)
                    .getSpanCount();
        }
        return spanCount;
    }
}
